﻿﻿﻿require.config({
  paths: {
    'jquery': '/js/packages/jquery-2.14-min',
    'bootstrap': '/bootstrap/js/bootstrap.min'
  },
  shim: {
    'bootstrap': {
      deps: ['jquery'],
      exports: 'bootstrap'
    }
  }
  
});
require(['jquery','bootstrap'], function ($) {
	var searchQuery="";
	var searchCountry="";
	$('#search-query').removeAttr("readonly");
	
	$('#search-rule').click(function(){
		$('.mtb10>table').hide('fast');
		$('.table-first>table').show('fast');
	});
	$('#search-field').click(function(){
		$('.mtb10>table').hide('fast');
		$('.table-second>table').show('fast');
	});
	
	$('table>tbody>tr>td>a').click(function(){
		searchQuery = searchQuery+$(this).text();
		makeQuery(searchQuery+searchCountry);
		
	});
	$('#search-start').click(function(){
		search_wx();
	});
	$("#clear-query").click(function(){
		$('textarea').val("");
	});
	$("li>label>input").on("change",function(argument){
		searchCountry = searchCon();
		makeQuery(searchQuery+searchCountry);
	});
	function makeQuery(curr) {
		$('textarea').val(curr);
	};
	function search_wx() {
		searchQuery = $('textarea').val();
		$.ajax({
			url:'/search/wx',
		    type:'POST', //GET
		    async:true,    //或false,是否异步
		    data:{
		    	query:searchQuery
		    },
		    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
		    success:function(data,textStatus,jqXHR){
		    	window.location.href="/patents/patentslist.html";
		    	console.log(data)
		        console.log(textStatus)
		        console.log(jqXHR)
		    },
		    error:function(xhr,textStatus){
		        console.log('错误')
		        console.log(xhr)
		        console.log(textStatus)
		    },
		    complete:function(){
		        console.log('结束')
		    }
		});
	};
	
	function searchCon() {
		var searchValue = "";
		var curr_con = "";
		var s_con = "";
		var s_pt=[];
		$('ul.root>li>ul>li>label>input').each(function(index){
			if($(this).prop("checked") ==true){
				var pt = $(this).attr("data-pt");
				var co=$(this).parent('label').parent('li').parent('ul').parent('li').find('>label>input').attr('data-co');
				if(curr_con==''||curr_con==co) {
					curr_con = co
					s_pt.push(pt);
				}else {
					
					if(s_pt.length==3) {
						if(searchValue=='') {
							searchValue = 'co='+'("'+curr_con+'")'
						}else {
							searchValue = searchValue+" OR "+'co='+'("'+curr_con+'")'
						}
					}else {
						ss_pt = ''
						for(var k in s_pt) {
							
							if(k==s_pt.length-1) {
								ss_pt = ss_pt+' "'+s_pt[k]+'"'
							}else {
								ss_pt = ss_pt+'"'+s_pt[k]+'" OR ';
							}
						}
						ss_pt = 'pt=('+ss_pt+')';
						if(searchValue=='') {
							searchValue = '('+'co='+'("'+curr_con+'")'+' AND '+ss_pt +')'
						}else {
							searchValue = searchValue+" OR ("+'co='+'("'+curr_con+'")'+' AND '+ss_pt +')'
						}
					}
					curr_con = co;
					s_pt = [];
					s_pt.push(pt);
				}
			}
		});
		
		if(s_pt.length==3) {
			if(searchValue=='') {
				searchValue = 'co='+'("'+curr_con+'")'
			}else {
				searchValue = searchValue+" OR "+'co='+'("'+curr_con+'")'
			}
		}else if(s_pt.length!=0){
			ss_pt = ''
			for(var k in s_pt) {
				if(k==s_pt.length-1) {
					ss_pt = ss_pt+'"'+s_pt[k]+'"'
				}else {
					ss_pt = ss_pt+'"'+s_pt[k]+'" OR ';
				}
			}
			ss_pt = 'pt=('+ss_pt+')';
			if(searchValue=='') {
				searchValue = '('+'co='+'("'+curr_con+'")'+' AND '+ss_pt +')'
			}else {
				searchValue = searchValue+" OR ("+'co='+'("'+curr_con+'")'+' AND '+ss_pt +')'
			}
		}
		$('ul.sroot>li>label>input').each(function(index){
			if($(this).prop("checked") ==true){
				//searchValue = searchValue+" OR "+'co='+'("'+curr_con+'")';
				curr_con = $(this).attr('data-co');
				if(searchValue=='') {
					searchValue = 'co='+'("'+curr_con+'")'
				}else {
					searchValue = searchValue+" OR "+'co='+'("'+curr_con+'")'
				}
			}
		});
		
		$('ul.other-root>li>ul>li>label>input').each(function(index){
			if($(this).prop("checked") ==true){
				//searchValue = searchValue+" OR "+'co='+'("'+curr_con+'")';
				curr_con = $(this).attr('data-co');
				if(searchValue=='') {
					searchValue = 'co='+'("'+curr_con+'")'
				}else {
					searchValue = searchValue+" OR "+'co='+'("'+curr_con+'")'
				}
			}
		});
		
		
		return searchValue;
	};
});


/*

$('#build-query').click(function(){
		searchQuery = makeQuery();
		$('#search-query').val(searchQuery);
	});
	$('#search-start').click(function(){
		search_wx();
	});
	search_dict = [];
	var searchValue = "";
	function init_searchQuery() {
		$.ajaxSetup({   
            async : false  
        }); 
		$.get('/search/query/history',
		{
			'pageNumber':'0',
			'pageSize':'20'
		},
		function(data,status){
			show_querys(data.entries);
			//show_pages(data.page,data.totalPages)
			
		});
	};
	function search_wx() {
		searchQuery = makeQuery();
		searchQuery = convertQuery(searchQuery);
		$.ajax({
			url:'/search/wx',
		    type:'POST', //GET
		    async:true,    //或false,是否异步
		    data:{
		    	query:searchQuery
		    },
		    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
		    success:function(data,textStatus,jqXHR){
		    	window.location.href="/patents/patentslist.html";
		    	console.log(data)
		        console.log(textStatus)
		        console.log(jqXHR)
		    },
		    error:function(xhr,textStatus){
		        console.log('错误')
		        console.log(xhr)
		        console.log(textStatus)
		    },
		    complete:function(){
		        console.log('结束')
		    }
		});
	};
	function saveQuery() {
		query = '';
		if($('#search-query')[0].innerHTML!='') {
			query = $('#search-query')[0].innerHTML;
		}else {
			query = makeQuery();
		}
		$.ajaxSetup({   
            async : false  
        }); 
		$.post('/search/query/save',
		{
			'query':query
		},
		function(data,status){
			alert('save ok!!')
		});
	};
	function convertQuery(searchQuery) {     
		while(searchQuery.match(/#[0-9]+/g)!=null) {
			searchQuery = searchQuery.replace(/#[0-9]+/g,function(a){
			    //arguments中的每个元素对应一个参数
				return search_dict[parseInt(a.substring(1))];
			  });
		}
		return searchQuery;
	}
	
	function makeQuery() {
		searchQuery = '';
		search_dict = [];
		search_dict.push(0);
		$('tbody>tr>td>input').each(function(index){
			search_dict.push('( '+$(this).parent('td').parent('tr').find('>td.query-value>div')[0].innerHTML+' )');
			if($(this).prop("checked") ==true){
				if(searchQuery == '') {
					searchQuery = $(this).parent('td').parent('tr').find('>td.query-index>div')[0].innerHTML;
				}else {
					searchQuery = searchQuery + ' AND ' +$(this).parent('td').parent('tr').find('>td.query-index>div')[0].innerHTML
				}
			}
		});
				
		return searchQuery;
	};
	function show_querys(query_data) {
		$('#querys-all').innerHTML='';
		for(var i in query_data) {
			addtd(query_data[i],i+1)
		}
	}
	function addtd(query,curr){
		
		var div_start_index = "<div class = 'search-index'>";
		var div_start_value = "<div class = 'search-content'>";
		var div_start_value = "<div class = 'search-action'>";
		var div_end = "</div>";
		var tr = document.createElement("tr");
		tr.setAttribute('id','tr-'+patent.source+'-'+patent.id);//source-id//WX//FILE
		var td0 = document.createElement("td");
		td0.innerHTML='<input id="query-'+query.id+'" type="checkbox">';
		var td1 = document.createElement("td");
		td1.setAttribute("class","query-index");
		td1.innerHTML=div_start_index+'#'+curr+div_end;//公开公告号
		var td2 = document.createElement("td");
		td2.setAttribute("class","query-action");
		td2.innerHTML=div_start_value+query.v+div_end;//名称
		var td3 = document.createElement("td");
		td3.setAttribute("class","patent-data-table show-column3");
		td3.innerHTML=div_start_action+'<span id="query-trash-'+query.id+'" class="glyphicon glyphicon-trash"></span>'+div_end;//申请（专利权）人
		tr.appendChild(td0);
		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		$('#querys-all').append(tr);
		$('#query-trash-'+query.id).click(function(){
			alert('trash'+$(this).id);
		});	
	}
*/
